<?php
/**
 * In this file the class \UF\Drawing\ContentAlign is defined.
 *
 * @category   UniKap-Framework
 * @package    Drawing
 * @subpackage Core
 * @author     Ulf -UniKado- Kadner <ulfikado@gmail.com>
 * @since      2014-04-04 16:17:16
 * @version    0.1
 */

namespace UF\Drawing
{

    /**
     * Defines the content align of a unspecified graphical element.
     *
     * @since  v0.1
     */
    class ContentAlign implements \UF\IToStringConverter
    {

        # <editor-fold defaultstate="collapsed" desc="- - - -   C O N S T A N T S   - - - - - - - - - - - - - - - - - - - - - - - - - -">

        /**
         * Content is aligned to the bottom right corner
         *
         * @since  v0.1
         */
        const BOTTOM_RIGHT = 0;

        /**
         * Content is aligned to the bottom corner, horizontaly centered.
         *
         * @since  v0.1
         */
        const BOTTOM = 1;

        /**
         * Content is aligned to the bottom left corner
         *
         * @since  v0.1
         */
        const BOTTOM_LEFT = 2;

        /**
         * Content is aligned to the right corner, verticaly centered
         *
         * @since  v0.1
         */
        const MIDDLE_RIGHT = 3;

        /**
         * Content is aligned horicontaly and verticaly centered
         *
         * @since  v0.1
         */
        const MIDDLE = 4;

        /**
         * Content is aligned to the left corner, verticaly centered
         *
         * @since  v0.1
         */
        const MIDDLE_LEFT = 5;

        /**
         * Content is aligned to the top right corner
         *
         * @since  v0.1
         */
        const TOP_RIGHT = 6;

        /**
         * Content is aligned to the top corner, horizontaly centered.
         *
         * @since  v0.1
         */
        const TOP = 7;

        /**
         * Content is aligned to the top left corner
         *
         * @since  v0.1
         */
        const TOP_LEFT = 8;

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="- - - -   P U B L I C   F I E L D S   - - - - - - - - - - - - - - - - - - - - - -">

        /**
         * The current align value (see the {@see \UF\Drawing\ContentAlign} class constants.
         *
         * @var    int
         * @since  v0.1
         */
        public $Value;

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="- - - >   C O N S T R U C T O R   - - - - - - - - - - - - - - - - - - - - - - - -">

        /**
         * Inits a new instance.
         *
         * @param  int $value The align value. (Defaults to {@see UF\Drawing\ContentAlign::BOTTOM_RIGHT})
         * @since  v0.1
         */
        public function __construct( $value = self::BOTTOM_RIGHT )
        {
            if ( ! self::isValidValue( $value ) )
            {
                if ( !\is_string( $value ) ) $this->Value = self::BOTTOM_RIGHT;
                elseif ( \preg_match( '~^[0-8]$~', $value ) ) $this->Value = \intval( $value );
                else $this->Value = self::BOTTOM_RIGHT;
            }
            else $this->Value = $value;
        }

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="- - - -   P U B L I C   M E T H O D S   - - - - - - - - - - - - - - - - - - - - -">

        /**
         * @return string Returns the current content align int value as a string
         * @since  v0.1
         */
        public function __toString()
        {
            return \strval( $this->Value );
        }

        /**
         * @return string Returns the current content align int value as a string
         * @since  v0.1
         */
        public function toString()
        {
            return (string) $this;
        }

        /**
         * Returns one of the \GRAVITY_* constants, associated to current content align.
         *
         * @return string|integer Returns a string if the Imagick extension isnt usable, a integer value otherwise.
         * @since  v0.1
         */
        public function toGravity()
        {
            switch ( $this->Value )
            {
                case self::TOP_LEFT:     return \GRAVITY_TOPLEFT;
                case self::TOP:          return \GRAVITY_TOP;
                case self::TOP_RIGHT:    return \GRAVITY_TOPRIGHT;
                case self::MIDDLE_LEFT:  return \GRAVITY_LEFT;
                case self::MIDDLE:       return \GRAVITY_CENTER;
                case self::MIDDLE_RIGHT: return \GRAVITY_RIGHT;
                case self::BOTTOM_LEFT:  return \GRAVITY_BOTTOMLEFT;
                case self::BOTTOM:       return \GRAVITY_BOTTOM;
                default:                 return \GRAVITY_BOTTOMRIGHT;
            }
        }

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="- - - -   P R I V A T E   S T A T I C   M E T H O D S   - - - - - - - - - - - - -">

        /**
         * @since  v0.1
         */
        private static function isValidValue( $value )
        {
            if ( !\is_int( $value ) ) return false;
            return -1 < $value && 9 > $value;
        }

        # </editor-fold>

    }

}

